package Project1;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import java.sql.Date;
import java.util.List;
import java.io.Serializable;

@Entity
@Table(name = "titles")
public class Titles implements Serializable {
	private long emp_no;
	private String title; 
	private Date from_date; 
	private Date to_date; 
	private Employee employee;
	
	
	

	public Titles() {}; 
	public Titles(long emp_no, String title, Date from_date, Date to_date){
		this.emp_no = emp_no; 
		this.title = title;
		this.from_date = from_date; 
		this.to_date = to_date;
		
	}
	
	//******************GETTERS SETTERS******************************
	 @Id
	 @Column(name="emp_no")
	 public long getempNo() { return emp_no; }
	 public void setEmpNo(long emp_no) {this.emp_no=emp_no;}
	 
	 @Id
	 @Column(name="title")
	 public String getTitle() { return title; }
	 public void setTitle(String title) {this.title = title;}
		
    @Id
    @Column(name="from_date")		  
    public Date getFrom_date() { return from_date; }
  	public void setFrom_date(Date from_date) {this.from_date=from_date;}
  	
  	@Column(name="to_date")
	public Date getTo_date() { return to_date; }
    public void setTo_date(Date to_date) {this.to_date=to_date;}
	
    @ManyToOne
	@JoinColumn(name="emp_no")
	public Employee getEmployee() {return employee;}
	public void setEmployee(Employee employee) {this.employee = employee;}
	
	//**************************METHODS********************************
    public void print()
    {
        System.out.printf("%d: %s %s %s\n", emp_no, title, from_date.toString(),to_date.toString()); 
    } 
    
    //**********************STATIC METHODS*********************************
    public static void list()
    {
        Session session = HibernateContext.getSession();
        Criteria criteria = session.createCriteria(Titles.class);
        criteria.addOrder(Order.asc("title"));
        
        List<Titles> titles = criteria.list();
        System.out.println("All Titles:");
        
        for (Titles title : titles) {
            title.print();
        }
        
        session.close();
    }
  
	
}
